package com.fzshang.shopping.wx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.Address;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-10
 */
@Controller
@RequestMapping("wx/address")
public class WXAddressController extends BaseController {

    @RequestMapping(value = "newAddress.html")
    public String address(Model model,Long userId){
        model.addAttribute("userId",userId);
        return "wx/personal/address/newAddress";
    }
    @RequestMapping(value = "updateAddress.html")
    public String updateAddress(Model model,Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Address address = addressService.getOne(queryWrapper);
        model.addAttribute("address",address);
        return "wx/personal/address/updateAddress";
    }

    /**
     * @param address
     * @return 添加
     */
    @RequestMapping(value = "saveOrUpdateById")
    @ResponseBody
    public ApiResult saveOrUpdateById(@RequestBody Address address,HttpServletRequest request){
        address.setCreateTime(System.currentTimeMillis());
        //添加默认,先查询该用户是否有默认地址
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("user_id",getWxUser(request).getId());
        queryWrapper1.eq("state",1);
        Address address1 = addressService.getOne(queryWrapper1);
        //该用户有默认地址,先把默认地址改为不是默认的在修改为默认
        if(address1!=null){
            address1.setState(2);
            if(!addressService.updateById(address1)){
                return ApiResult.fail();
            }
        }

        if(addressService.saveOrUpdate(address)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(addressService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 修改状态
     */
    @RequestMapping(value = "updateState")
    @ResponseBody
    public ApiResult updateState(Long id, HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);
        Address address = addressService.getOne(queryWrapper);
        if(address.getState()==1){
            address.setState(2);
            if(addressService.updateById(address)){
                return ApiResult.ok();
            }
        }else{
            //修改默认,先查询该用户是否有默认地址
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("user_id",getWxUser(request).getId());
            queryWrapper1.eq("state",1);
            Address address1 = addressService.getOne(queryWrapper1);
            //该用户有默认地址,先把默认地址改为不是默认的在修改为默认
           if(address1!=null){
               address1.setState(2);
               if(!addressService.updateById(address1)){
                   return ApiResult.fail();
               }
           }
           address.setState(1);
            if(addressService.updateById(address)){
                return ApiResult.ok();
            }
        }
        return ApiResult.fail();
    }

}
